package com.artPreter.service.impl;


import com.artPreter.mapper.ArtworkMapper;
import com.artPreter.mapper.IndexItemRelativeMapper;
import com.artPreter.mapper.IndexTypeMapper;
import com.artPreter.pojo.*;
import com.artPreter.service.IIndexItemRelativeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 首页板块-内容关系表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-12-10
 */
@Service
public class IndexItemRelativeServiceImpl extends ServiceImpl<IndexItemRelativeMapper, IndexItemRelative> implements IIndexItemRelativeService {

    @Autowired
    private IndexTypeMapper indexTypeMapper;


    @Override
    public List<Artwork> getIndexContentArtworks(String indexItemId,Integer size) {
        List<Artwork> artworks = new ArrayList<>();
        QueryWrapper<Artwork> artworkQueryWrapper = new QueryWrapper<>();
        artworkQueryWrapper.eq("b.index_item_id",indexItemId);
        artworkQueryWrapper.orderByDesc("b.sort");
        artworkQueryWrapper.last(" limit " + size + " ");
        artworks = this.baseMapper.getIndexContentArtworks(artworkQueryWrapper);
        return artworks;
    }

    @Override
    public List<EventExhibition> getIndexContentEventExhibitions(String indexItemId, Integer size) {

        QueryWrapper<EventExhibition> eventExhibitionQueryWrapper = new QueryWrapper<>();
        // 有效关联
        eventExhibitionQueryWrapper.isNotNull("s.id");
        // 板块id
        eventExhibitionQueryWrapper.eq("s.index_item_id",indexItemId);
        // 名称
        eventExhibitionQueryWrapper.eq("b.status",1);
        List<EventExhibition> list = this.baseMapper.getIndexContentEventExhibitions(eventExhibitionQueryWrapper);
        for (int l = 0; l < list.size(); l++) {
            if(list.get(l).getEndTime().isBefore(LocalDateTime.now())){
                list.get(l).setIsOver(1);
            }
            List<Artist> list1 = new ArrayList<>();
            if(list.get(l).getArtistIds()!=null){
                String[] strings1 = list.get(l).getArtistIds().split("，");
                String[] strings2 = list.get(l).getArtistNames().split("，");
                for (int z = 0; z < strings1.length; z++) {
                    if(!strings1[z].equals("")){
                        Artist artist = new Artist();
                        artist.setId(Integer.valueOf(strings1[z]));
                        artist.setName(strings2[z]);
                        list1.add(artist);
                    }

                }
            }
            list.get(l).setList(list1);
        }
        return list;

    }
}
